﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Chronos;
using System;
using QxFramework.Utilities;
using QxFramework.Core;

public class SlimeAttack : SkillBase
{
    public float damageRatio = 1;
    public RigidbodyTimeline2D rb;
    public float attackRange = 1;

    public override void Init(EntityBase user)
    {
        base.Init(user);
        ColdDownInit();

        rb = User.time.rigidbody2D;

        //MessageManager.Instance.Get<SlimeAttack_Event>().RegisterHandler(SlimeAttack_Event.Stop, Stop);
        //MessageManager.Instance.Get<SlimeAttack_Event>().RegisterHandler(SlimeAttack_Event.Damage, Damage);
        //MessageManager.Instance.Get<SlimeAttack_Event>().RegisterHandler(SlimeAttack_Event.PlayEffect, PlayEffect);

        //PlayEffect(null,null);
    }
    public override void FixedUpdate()
    {
        if (!isColddown)
        {
            if((User.parameter.AimData.target.position - User.transform.position).magnitude < attackRange)
            {
                //Debug.Log("attack");
                rb.velocity = Vector2.zero;
                User.ChangeState(States.SlimeAttackState);
                
                

                isColddown = true;
            }
            
        }

        ColdDownCheck();
    }
    public void Stop(object sender, EventArgs args)
    {
        User.ChangeState(States.StandState);
    }

    public override void Damage(EventArgs args)
    {
        if (args is AttackInfo arg)
        {
            foreach (EntityBase player in arg.targets)
            {
                if (player.tag == "Player")
                    player.parameter.HealthData.currentHealth -= User.parameter.StatusPointData.attack * damageRatio;
            }
        }
        //var enemys = Physics2D.BoxCastAll(Player.transform.position, new Vector2(1,1), 0,Player.parameter.MovementData.moveDir);
        Debug.Log("hit!");
    }
    //public void PlayEffect(object sender, EventArgs args)
    //{
    //    EffectManager.Instance.InstantiateSkillEffect("SlimeAttack", EffectManager.Instance.transform.GetChild(0), this.User.transform.position);
    //}
    
}
public enum SlimeAttack_Event
{
    Damage,
    Stop,
    PlayEffect,
}
